package com.nccsjz.back.vote.service;

import java.sql.SQLException;
import java.util.List;

import com.nccsjz.back.vote.dao.VoteDAO;
import com.nccsjz.base.BaseService;
import com.nccsjz.pojo.Vote;
import com.nccsjz.pojo.VoteAnswer;

/**
 * VoteService类用于选票业务处理
 * 
 * @author jason
 * 
 */
public class VoteService extends BaseService {

	/**
	 * getAllVotes方法获取所有选票信息
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List<Vote> getAllVotes() throws SQLException {
		List<Vote> votes = null;
		try {
			openCon();
			VoteDAO voteDAO = new VoteDAO(con);
			votes = voteDAO.queryVotes();
			for (Vote v : votes) {
				List<VoteAnswer> voteAnswers = voteDAO.queryAnswersByVoteId(v);
				v.setAnswers(voteAnswers);
			}
		} finally {
			closeCon();
		}
		return votes;
	}
	
	/**
	 * addVote方法用于保存投票问题
	 * 
	 * @param vote Vote	投票对象
	 * @throws SQLException 
	 */
	public synchronized void saveVote(Vote vote) throws SQLException{
		try{
			openCon();
			con.setAutoCommit(false);
			VoteDAO voteDAO = new VoteDAO(con);
			//1.检查新增选票是否指定为投票
			if(vote.getState() == 1){
				//设置所有其他选票的状态为0
				voteDAO.updateAllVoteState(0);
			}
			//2.插入选票信息
			voteDAO.insertVote(vote);
			Long voteId = voteDAO.queryMaxVoteId();
			//3.插入选票答案信息
			List<VoteAnswer> answers = vote.getAnswers();
			for(VoteAnswer answer:answers){
				answer.getVote().setVoteId(voteId);
				voteDAO.insertVoteAnswer(answer);
			}
			con.commit();
		}catch(Exception e){
			con.rollback();
			e.printStackTrace();
		}finally{
			closeCon();
		}		
	}

	/**
	 * removeVoteById方法根据编号删除选票信息
	 * 
	 * @param voteId
	 * @throws SQLException 
	 */
	public void removeVoteById(Long voteId) throws SQLException{
		try{
			openCon();
			con.setAutoCommit(false);
			VoteDAO voteDAO = new VoteDAO(con);			
			//1.删除选票答案
			voteDAO.deleteVoteAnswerByVoteId(voteId);
			//2.删除选票
			voteDAO.deleteVoteByVoteId(voteId);
			con.commit();
		}catch(Exception e){
			e.printStackTrace();
			con.rollback();
		}finally{
			closeCon();
		}
	}
	
	/**
	 * setVotedState方法将指定选票设置为投票 
	 * 
	 * @param votedId
	 * @throws SQLException 
	 */
	public void setVotedState(Long voteId) throws SQLException{
		try{
			openCon();
			con.setAutoCommit(false);
			VoteDAO voteDAO = new VoteDAO(con);
			voteDAO.updateAllVoteState(0);
			voteDAO.updateVoteStateById(voteId, 1);
			con.commit();
		}catch(Exception e){
			e.printStackTrace();
			con.rollback();
		}finally{
			closeCon();
		}
	}
	
	/**
	 * findVotesByVoteId方法用于根据选票编号查询选票信息
	 * 
	 * @param voteId
	 * @return
	 * @throws SQLException 
	 */
	public Vote findVoteByVoteId(Long voteId) throws SQLException{
		Vote v = null;
		try{
			openCon();
			VoteDAO voteDAO = new VoteDAO(con);
			v = voteDAO.queryVoteById(voteId);
			List<VoteAnswer> answers = voteDAO.queryAnswersByVoteId(v);
			v.setAnswers(answers);
		}finally{
			closeCon();
		}		
		return v;
	}
	
	/**
	 * editVote用于更新选票内容
	 * 
	 * @param v
	 * @throws SQLException 
	 */
	public void editVote(Vote v) throws SQLException{
		try{
			openCon();
			con.setAutoCommit(false);
			VoteDAO voteDAO = new VoteDAO(con);
			//1.检查选票是否设定为投票
			if(v.getState() == 1){
				voteDAO.updateAllVoteState(0);
			}
			//2.更新选票信息
			voteDAO.updateVoteById(v);
			//3.更新选票答案信息
			List<VoteAnswer> answers = v.getAnswers();
			for(VoteAnswer a : answers){
				voteDAO.updateVoteAnswerById(a);
			}
			con.commit();
		}catch(Exception e){
			e.printStackTrace();
			con.rollback();
		}finally{
			closeCon();
		}
	}
	/**
	 * updateAnswerClickPlusOne方法用于更新投票答案的点击数量
	 * 
	 * @param voteAnswerId	投票答案编号
	 * @throws SQLException 
	 */
	public void updateAnswerClickCountPlusOne(Long voteAnswerId) throws SQLException{
		this.openCon();
		new VoteDAO(con).updateAnswerClickCountPlusOne(voteAnswerId);
		this.closeCon();
	}
}
